//
//  RTVideoLandView.m
//  StoryToy
//
//  Created by roobo on 2018/5/9.
//  Copyright © 2018年 roobo. All rights reserved.
//

#import "RTVideoLandView.h"
#import "RTVoiceSwitch.h"
#import "RTVideoMainController.h"
#import <Masonry.h>

@interface RTVideoLandView ()<UIGestureRecognizerDelegate>

@end

@implementation RTVideoLandView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
//        UIImageView *backImageView = [UIImageView new];
//        [self addSubview:backImageView];
//        backImageView.userInteractionEnabled = YES;
//        backImageView.image = [UIImage imageNamed:@"full_pic_video"];
//        [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.mas_equalTo(self);
//        }];
//        _backImageView = backImageView;
        
        UIButton *fullVideoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:fullVideoBtn];
        _fullVideoBtn = fullVideoBtn;
        [fullVideoBtn setImage:[UIImage imageNamed:@"icon_narrow_nor"] forState:UIControlStateNormal];
        [fullVideoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-25);
            make.bottom.mas_equalTo(-25);
            make.height.width.mas_equalTo(40);
        }];
        
        UIButton *videoCntBtn= [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:videoCntBtn];
        _videoCntBtn = videoCntBtn;
        [videoCntBtn setImage:[UIImage imageNamed:@"btn_full_connect_nor"] forState:UIControlStateNormal];
        [videoCntBtn setImage:[UIImage imageNamed:@"btn_full_connect_pre"] forState:UIControlStateSelected];
        [videoCntBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(64);
            make.height.mas_equalTo(63);
            make.bottom.mas_equalTo(-25);
            make.centerX.mas_equalTo(self.mas_centerX);
        }];
        
        RTVoiceSwitch *speakSwitch = [[RTVoiceSwitch alloc] initWithFrame:CGRectMake(25, [[UIScreen mainScreen]bounds].size.width-40-25, 80,40)];
        speakSwitch.backgroundColor = [UIColor clearColor];
        [speakSwitch setOn:NO animated:YES];
        _speakSwitch = speakSwitch;
        [self addSubview:speakSwitch];
        
        UIButton *fullBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:fullBackBtn];
        _fullBackBtn = fullBackBtn;
        [fullBackBtn setImage:[UIImage imageNamed:@"video_back_nor"] forState:UIControlStateNormal];
        [fullBackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(25);
            make.top.mas_equalTo(25);
            make.height.width.mas_equalTo(40);
        }];
        
        UIButton *fullRecordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:fullRecordBtn];
        _fullRecordBtn = fullRecordBtn;
        [fullRecordBtn setImage:[UIImage imageNamed:@"icon_video_nor"] forState:UIControlStateNormal];
        [fullRecordBtn setImage:[UIImage imageNamed:@"icon_video_sel"] forState:UIControlStateSelected];
        
        [fullRecordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX);
            make.top.mas_equalTo(fullBackBtn.mas_top);
            make.height.width.mas_equalTo(40);
        }];
        
        UIButton *fullAlbumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:fullAlbumBtn];
        _fullAlbumBtn = fullAlbumBtn;
        [fullAlbumBtn setImage:[UIImage imageNamed:@"icon_pic_nor"] forState:UIControlStateNormal];
        [fullAlbumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(fullBackBtn.mas_top);
            make.right.mas_equalTo(fullRecordBtn.mas_left).offset(-20);
            make.height.width.mas_equalTo(40);
        }];
        
        UIButton *fullPictBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:fullPictBtn];
        _fullPictBtn = fullPictBtn;
        [fullPictBtn setImage:[UIImage imageNamed:@"icon_photo_nor"] forState:UIControlStateNormal];
        [fullPictBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(fullBackBtn.mas_top);
            make.left.mas_equalTo(fullRecordBtn.mas_right).offset(20);
            make.height.width.mas_equalTo(40);
        }];
        
        //设置视频的清晰度
        UIButton *definiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:definiBtn];
        [definiBtn addTarget:self action:@selector(showDefiniView) forControlEvents:UIControlEventTouchUpInside];
        [definiBtn setImage:[UIImage imageNamed:@"icon_definition"] forState:UIControlStateNormal];
        [definiBtn setImage:[UIImage imageNamed:@"icon_clear2"] forState:UIControlStateSelected];
        [definiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(0).offset(-25);
            make.top.mas_equalTo(20);
            make.height.width.mas_equalTo(49);
        }];
        _definiBtn = definiBtn;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDefiniView)];
        tapGes.delegate = self;
        [self addGestureRecognizer:tapGes];
        
        self.videoDefini = RTVideoDefini_1280_720;
    }
    return self;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if(touch.view == _definiView){
        return NO;
    }
    return YES;
}

- (void)setVideoDefini:(RTVideoDefini)videoDefini {
    _videoDefini = videoDefini;
    if (videoDefini == RTVideoDefini_640_480) {
        [_higDefBtn setSelected:NO];
        [_norDefBtn setSelected:YES];
        [_definiBtn setSelected:YES];
    }
    if (videoDefini == RTVideoDefini_1280_720) {
        [_higDefBtn setSelected:YES];
        [_norDefBtn setSelected:NO];
        [_definiBtn setSelected:NO];
    }
}

-(void)hideDefiniView{
    if (_definiView) {
        _definiView.hidden = YES;
        //        [_definiView removeFromSuperview];
        //        _definiView = nil;
    }
}

-(void)showDefiniView{
    if (self.videoVC.isLiving) {
        if (!_definiView) {
            UIView *definiView = [UIView new];
            definiView.frame = CGRectMake([[UIScreen mainScreen]bounds].size.height-150,0, 150, [[UIScreen mainScreen]bounds].size.width);
            definiView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
            [self addSubview:definiView];
            _definiView = definiView;
            
            UIButton *higDefBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [definiView addSubview:higDefBtn];
            [higDefBtn addTarget:self action:@selector(setHightDefView) forControlEvents:UIControlEventTouchUpInside];
            [higDefBtn setBackgroundImage:[UIImage imageNamed:@"icon_clean_blue"] forState:UIControlStateSelected];
            [higDefBtn setTitle:@"清晰" forState:UIControlStateNormal];
            higDefBtn.titleLabel.font = [UIFont systemFontOfSize:16];
            [higDefBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [higDefBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(_definiView.mas_centerY).offset(-30);
                make.centerX.mas_equalTo(_definiView.mas_centerX);
                make.width.mas_equalTo(50);
                make.height.mas_equalTo(25);
            }];
            _higDefBtn = higDefBtn;
            
            UIButton *norDefBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [definiView addSubview:norDefBtn];
            [norDefBtn addTarget:self action:@selector(setNorDefView) forControlEvents:UIControlEventTouchUpInside];
            [norDefBtn setBackgroundImage:[UIImage imageNamed:@"icon_clean_blue"] forState:UIControlStateSelected];
            [norDefBtn setTitle:@"流畅" forState:UIControlStateNormal];
            norDefBtn.titleLabel.font = [UIFont systemFontOfSize:16];
            [norDefBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [norDefBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(_definiView.mas_centerY).offset(30);
                make.centerX.mas_equalTo(_definiView.mas_centerX);
                make.width.mas_equalTo(50);
                make.height.mas_equalTo(25);
            }];
            _norDefBtn = norDefBtn;
            
            self.videoDefini = self.videoDefini;
            
        }else {
            _definiView.hidden = NO;
        }
    }else {
        NSLog(@"请开启监护");
        [SVProgressHUD showErrorWithStatus:@"请开启监护"];
    }
}

-(void)setHightDefView{
    self.videoDefini = RTVideoDefini_1280_720;
    [self hideDefiniView];
    if (self.exchangeDefinitionBlock) {
        self.exchangeDefinitionBlock(_videoDefini);
    }
}

-(void)setNorDefView{
    self.videoDefini = RTVideoDefini_640_480;
    [self hideDefiniView];
    if (self.exchangeDefinitionBlock) {
        self.exchangeDefinitionBlock(_videoDefini);
    }
}

@end

